window.adwiser = window.adwiser || {};
adwiser.one = function(){
        var d = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        var c = d.length;
        return d.charAt(Math.floor(Math.random() * c));
};
adwiser.gv = {};
adwiser.random = function(c){
        var d = c || 8;
        var e = "";
        while (d--) {
            e += adwiser.one();
        }
        if (!adwiser.gv[e]) {
            adwiser.gv[e] = 1;
            return e;
        } else {
            return adwiser.random(d);
        }
};

adwiser.isString = function (a) {
    return (typeof a == "object" && a && a.constructor == String) || typeof a == "string";
};
adwiser.$ = function () {
    for (var b = [], c = arguments.length - 1; c > -1; c--) {
        var d = arguments[c];
        b[c] = null;
        if (typeof d == "object" && d && d.dom) {
            b[c] = d.dom;
        } else {
            if ((typeof d == "object" && d && d.tagName) || d == window || d == document) {
                b[c] = d;
            } else {
                if (adwiser.isString(d) && (d = document.getElementById(d))) {
                    b[c] = d;
                }
            }
        }
    }
    return b.length < 2 ? b[0] : b;
};
adwiser.format = function (h, j) {
    if (arguments.length > 1) {
        var e = adwiser.format,
            g = new RegExp("([.*+?^=!:\x24{}()|[\\]\\/\\\\])", "g"),
            f = (e.left_delimiter || "{").replace(g, "\\\x241"),
            a = (e.right_delimiter || "}").replace(g, "\\\x241");
        var c = e._r1 || (e._r1 = new RegExp("#" + f + "(.+?)" + a, "g")),
            b = e._r2 || (e._r2 = new RegExp("#" + f + "(\\d+)" + a, "g"));
        if (typeof(j) == "object" && !adwiser.isString(j)) {
            return h.replace(c, function (k, m) {
                var l = j[m];
                if (typeof l == "function") {
                    l = l(m);
                }
                return typeof(l) == "undefined" ? "" : l;
            });
        } else {
            if (typeof(j) != "undefined") {
                var i = Array.prototype.slice.call(arguments, 1);
                var d = i.length;
                return h.replace(b, function (k, l) {
                    l = parseInt(l, 10);
                    return (l >= d) ? k : i[l];
                });
            }
        }
    }
    return h;
};
adwiser.format.delimiter = function (c, a) {
    var b = adwiser.format;
    b.left_delimiter = c || "{";
    b.right_delimiter = a || c || "}";
    b._r1 = b._r2 = null;
};

adwiser.on = function (h, f, e, g) {
    if (!(h = adwiser.$(h)) || typeof e != "function") {
        return h;
    }
    f = f.replace(/^on/i, "").toLowerCase();

    function b(i) {
        return i || window.event;
    }
    var d = function (i) {
        e.call(d.src, b(i));
    };
    d.src = h;
    var c = adwiser.on._listeners;
    var a = [h, f, e, d];
    c[c.length] = a;
    if (h.attachEvent) {
        h.attachEvent("on" + f, d);
    } else {
        if (h.addEventListener) {
            h.addEventListener(f, d, false);
        }
    }
    return h;
};
adwiser.on._listeners = [];

adwiser.each = function (f, b) {
    if (typeof b != "function") {
        return f;
    }
    if (f) {
        var a;
        if (f.length === undefined) {
            for (var c in f) {
                if (c in {}) {
                    continue;
                }
                a = b.call(f[c], f[c], c);
                if (a == "break") {
                    break;
                }
            }
        } else {
            for (var d = 0, e = f.length; d < e; d++) {
                a = b.call(f[d], f[d], d);
                if (a == "break") {
                    break;
                }
            }
        }
    }
    return f;
};
adwiser.hide = function () {
    adwiser.each(arguments, function (a) {
        if (a = adwiser.$(a)) {
            a.style.display = "none";
        }
    });
};
adwiser.show = function () {
    adwiser.each(arguments, function (a) {
        if (a = adwiser.$(a)) {
            a.style.display = "";
        }
    });
};

adwiser.toggle = function () {
    this.each(arguments, function (a) {
        if (a = adwiser.$(a)) {
            a.style.display = a.style.display == "none" ? "" : "none";
        }
    });
};

adwiser.un = function (b, f, k) {
    if (!(b = this.$(b)) || typeof k != "function") {
        return b;
    }
    f = f.replace(/^on/i, "").toLowerCase();
    var g = adwiser.on._listeners;
    if (!k) {
        var h;
        for (var c = 0, e = g.length; c < e; c++) {
            h = g[c];
            if (h && h[0] === b && h[1] === f) {
                adwiser.un(b, f, h[2]);
            }
        }
        return b;
    }
    function a(o, n, q) {
        for (var p = 0, m = g.length; p < m; ++p) {
            var l = g[p];
            if (l && l[2] === k && l[0] === b && l[1] === f) {
                return p;
            }
        }
        return -1;
    }
    var d = a(b, f, k);
    var j = null;
    if (d >= 0) {
        j = g[d];
    } else {
        return b;
    }
    if (b.detachEvent) {
        b.detachEvent("on" + f, j[3]);
    } else {
        if (b.removeEventListener) {
            b.removeEventListener(f, j[3], false);
        }
    }
    delete j[3];
    delete j[2];
    g.splice(d, 1);
    return b;
};
///////////date

adwiser.date = adwiser.date || {};
adwiser.number = adwiser.number || {};
adwiser.number.pad = function (c, b) {
    var a, d = "";
    a = c.toString();
    if (typeof(b) != "number") {
        return a;
    }
    if (a.length < b) {
        d = new Array(b - a.length + 1);
        d = d.join("0");
    }
    return d + a;
};
adwiser.date.format = function (c, g) {
    if (typeof(g) != "string") {
        return null;
    }
    var a = function (m, l) {
        e = e.replace(m, l);
    };
    var b = adwiser.number.pad;
    var e = g;
    var h = c.getFullYear();
    var f = c.getMonth() + 1;
    var k = c.getDate();
    var i = c.getHours();
    var d = c.getMinutes();
    var j = c.getSeconds();
    a(/yyyy/g, b(h, 4));
    a(/yy/g, b(h.toString().slice(2), 2));
    a(/MM/g, b(f, 2));
    a(/M/g, f);
    a(/dd/g, b(k, 2));
    a(/d/g, k);
    a(/d/g, k);
    a(/HH/g, b(i, 2));
    a(/H/g, i);
    a(/hh/g, b(i % 12, 2));
    a(/h/g, i % 12);
    a(/mm/g, b(d, 2));
    a(/m/g, d);
    a(/ss/g, b(j, 2));
    a(/s/g, j);
    return e;
};
adwiser.date.parse = function (a) {
    var b = new RegExp("^\\d+(\\-|\\/)\\d+(\\-|\\/)\\d+\x24");
    if (typeof(a) == "string") {
        if (b.test(a) || isNaN(Date.parse(a))) {
            var c = a.split(/ |T/);
            if (c.length > 1) {
                c[1] = c[1].split(/[^\d]/);
            } else {
                c[1] = [0, 0, 0];
            }
            c[0] = c[0].split(/[^\d]/);
            return new Date(c[0][0] - 0, c[0][1] - 1, c[0][2] - 0, c[1][0] - 0, c[1][1] - 0, c[1][2] - 0);
        } else {
            return new Date(a);
        }
    } else {
        return new Date();
    }
};

///////dom

adwiser.dom = adwiser.dom || {};
adwiser.isElement = function (a) {
    if (a === undefined || a === null) {
        return false;
    }
    return a && a.nodeName && a.nodeType == 1;
};
adwiser.isDocument = function (a) {
    if (a === undefined || a === null) {
        return false;
    }
    return a && a.nodeType == 9;
};
adwiser.dom.getDocument = function (a) {
    if (adwiser.isElement(a) || adwiser.isDocument(a)) {
        return a.nodeType == 9 ? a : a.ownerDocument || a.document;
    } else {
        throw new Error("[adwiser.dom.getDocument] param must be Element or Document");
    }
};
adwiser.isElement = function (a) {
    return a && a.nodeType == 1;
};
adwiser.dom.getPosition = function (d) {
    d = adwiser.$(d);
    if (!adwiser.isElement(d)) {
        throw new Error("[adwiser.dom.getPosition] param must be Element");
    }
    var g = adwiser.dom.getDocument(d);
    var f = adwiser.isGecko > 0 && g.getBoxObjectFor && adwiser.getCurrentStyle(d, "position") == "absolute" && (d.style.top === "" || d.style.left === "");
    var h = {
        left: 0,
        top: 0
    };
    var b = (adwiser.ie && !adwiser.isStrict) ? g.body : g.documentElement;
    if (d == b) {
        return h;
    }
    var c = null;
    var e;
    if (d.getBoundingClientRect) {
        e = d.getBoundingClientRect();
        h.left = e.left + Math.max(g.documentElement.scrollLeft, g.body.scrollLeft);
        h.top = e.top + Math.max(g.documentElement.scrollTop, g.body.scrollTop);
        h.left -= g.documentElement.clientLeft;
        h.top -= g.documentElement.clientTop;
        if (adwiser.ie && !adwiser.isStrict) {
            h.left -= 2;
            h.top -= 2;
        }
    } else {
        if (g.getBoxObjectFor && !f) {
            e = g.getBoxObjectFor(d);
            var a = g.getBoxObjectFor(b);
            h.left = e.screenX - a.screenX;
            h.top = e.screenY - a.screenY;
        } else {
            c = d;
            do {
                h.left += c.offsetLeft;
                h.top += c.offsetTop;
                if (adwiser.isWebkit > 0 && adwiser.getCurrentStyle(c, "position") == "fixed") {
                    h.left += g.body.scrollLeft;
                    h.top += g.body.scrollTop;
                    break;
                }
                c = c.offsetParent;
            } while (c && c != d);
            if (adwiser.opera > 0 || (adwiser.isWebkit > 0 && adwiser.getCurrentStyle(d, "position") == "absolute")) {
                h.top -= g.body.offsetTop;
            }
            c = d.offsetParent;
            while (c && c != g.body) {
                h.left -= c.scrollLeft;
                if (!adwiser.opera || c.tagName != "TR") {
                    h.top -= c.scrollTop;
                }
                c = c.offsetParent;
            }
        }
    }
    return h;
};

adwiser.dom.children = function (b) {
    b = adwiser.$(b);
    if (!(adwiser.isElement(b) || adwiser.isDocument(b))) {
        throw new Error("[adwiser.dom.children] param must be Element or Document");
    }
    var a = [];
    adwiser.each(b.childNodes, function (c) {
        if (c.nodeType == 1) {
            a.push(c);
        }
    });
    return a;
};
adwiser.dom.contains = function (b, a) {
    b = adwiser.$(b);
    a = adwiser.$(a);
    if (!(adwiser.isElement(b) || adwiser.isDocument(b))) {
        throw new Error("[adwiser.dom.contains] parent_node param must be Element or Document");
    }
    if (!adwiser.isElement(a)) {
        throw new Error("[adwiser.dom.children] child_node param must be Element");
    }
    return b.contains ? b != a && b.contains(a) : !! (b.compareDocumentPosition(a) & 16);
};

adwiser.dom.setProperties = function (b, a) {
    b = adwiser.$(b);
    adwiser.each(a, function (d, c) {
        adwiser.setAttr(b, c, d);
    });
};
adwiser.dom.create = function (b, a) {
    b = String(b);
    if (b == "") {
        throw new Error("[adwiser.dom.create] param tagName can not be empty");
    }
    if (adwiser.ie && a && a.name) {
        b = "<" + b + ' name="' + adwiser.encodeHTML(a.name) + '">';
    }
    var c = document.createElement(b);
    if (a) {
        adwiser.dom.setProperties(c, a);
    }
    return c;
};
adwiser.dom.matchNode = function (b, a, d) {
    b = adwiser.$(b);
    if (adwiser.isElement(b) || adwiser.isDocument(b)) {
        var c = b[d];
        while (c) {
            if (c.nodeType == 1) {
                return c;
            }
            c = c[a];
        }
        return null;
    } else {
        throw new Error("[adwiser.dom.matchNode] param must be Element or Document");
    }
};
adwiser.dom.first = function (a) {
    return adwiser.dom.matchNode(a, "nextSibling", "firstChild");
};
adwiser.dom.getWindow = function (a) {
    if (!adwiser.isDocument(a)) {
        throw new Error("[adwiser.dom.getWindow] param must be Document");
    }
    return a.parentWindow ? a.parentWindow : (a.defaultView ? a.defaultView : null);
};
adwiser.dom.last = function (a) {
    return adwiser.dom.matchNode(a, "previousSibling", "lastChild");
};
adwiser.dom.loadCssFile = function (b, c) {
    var a = document.createElement("link");
    if (c) {
        a.id = c;
    }
    a.setAttribute("rel", "stylesheet");
    a.setAttribute("type", "text/css");
    a.setAttribute("href", b);
    document.getElementsByTagName("head")[0].appendChild(a);
};
adwiser.dom.loadJsFile = function (b) {
    var a = document.createElement("script");
    a.setAttribute("type", "text/javascript");
    a.setAttribute("src", b);
    a.setAttribute("defer", "defer");
    document.getElementsByTagName("head")[0].appendChild(a);
};
adwiser.dom.next = function (a) {
    return adwiser.dom.matchNode(a, "nextSibling", "nextSibling");
};
adwiser.dom.prev = function (a) {
    return adwiser.dom.matchNode(a, "previousSibling", "previousSibling");
};
adwiser.dom.ready = function () {
    var c = false,
        e = false,
        d = [];

    function a() {
        if (!c) {
            c = true;
            for (var g = 0, f = d.length; g < f; g++) {
                try {
                    d[g]();
                } catch (h) {}
            }
        }
    }
    function b() {
        if (e) {
            return
        }
        e = true;
        if (document.addEventListener && !adwiser.opera) {
            document.addEventListener("DOMContentLoaded", a, false);
        }
        if (adwiser.ie && window == top) {
            (function () {
                if (c) {
                    return
                }
                try {
                    document.documentElement.doScroll("left");
                } catch (g) {
                    setTimeout(arguments.callee, 0);
                    return
                }
                a();
            })();
        }
        if (adwiser.opera) {
            document.addEventListener("DOMContentLoaded", function () {
                if (c) {
                    return
                }
                for (var g = 0; g < document.styleSheets.length; g++) {
                    if (document.styleSheets[g].disabled) {
                        setTimeout(arguments.callee, 0);
                        return
                    }
                }
                a();
            }, false);
        }
        if (adwiser.safari) {
            var f;
            (function () {
                if (c) {
                    return
                }
                if (document.readyState != "loaded" && document.readyState != "complete") {
                    setTimeout(arguments.callee, 0);
                    return
                }
                if (f === undefined) {
                    f = 0;
                    var l = document.getElementsByTagName("style");
                    var h = document.getElementsByTagName("link");
                    if (l) {
                        f += l.length;
                    }
                    if (h) {
                        for (var k = 0, g = h.length; k < g; k++) {
                            if (h[k].getAttribute("rel") == "stylesheet") {
                                f++;
                            }
                        }
                    }
                }
                if (document.styleSheets.length != f) {
                    setTimeout(arguments.callee, 0);
                    return
                }
                a();
            })();
        }
        if (window.attachEvent) {
            window.attachEvent("onload", a);
        } else {
            window.addEventListener("load", a, false);
        }
    }
    return function (f) {
        if (typeof f != "function") {
            return false;
        }
        b();
        if (c) {
            f();
        } else {
            d[d.length] = f;
        }
    };
}();
adwiser.ready = adwiser.dom.ready;
adwiser.dom.remove = function (a) {
    a = adwiser.$(a);
    if (!adwiser.isElement(a)) {
        throw new Error("[adwiser.dom.remove] param must be Element");
    }
    if (adwiser.ie) {
        if (a && a.nodeName != "BODY") {
            var b = document.createElement("DIV");
            b.appendChild(a);
            b.innerHTML = "";
            b = null;
        }
    } else {
        if (a && a.parentNode && a.nodeName != "BODY") {
            a.parentNode.removeChild(a);
        }
    }
};
////////////////

adwiser.ie = /msie (\d+\.\d)/i.test(navigator.userAgent) ? parseFloat(RegExp["\x241"]) : 0;



















function stopDefault(a) {
    if (a && a.preventDefault) {
        a.preventDefault();
    } else {
        window.event.returnValue = false;
    }
    return false;
}
function stopBubble(a) {
    var a = a || window.event;
    if (a.stopPropagation) {
        a.stopPropagation();
    } else {
        window.event.cancelBubble = true;
    }
}

function formatDate(c, b) {
    if (typeof(c) != "string" && b.constructor != Date) {
        return null;
    }
    function a(e, f) {
        f = f.length;
        e = e || 0;
        var g = String(Math.pow(10, f) + e);
        return f == 1 ? e : g.substr(g.length - f);
    }
    return c.replace(/([YMDhsmw])\1*/g, function (d) {
        switch (d.charAt()) {
        case "Y":
            return a(b.getFullYear(), d);
        case "M":
            return a(b.getMonth() + 1, d);
        case "D":
            return a(b.getDate(), d);
        case "w":
            return b.getDay();
        case "h":
            return a(b.getHours(), d);
        case "m":
            return a(b.getMinutes(), d);
        case "s":
            return a(b.getSeconds(), d);
        }
    });
}
function DateSelector(b) {
    this.uniqueId = adwiser.random();
    b = b || {};
    this.onselect = b.onselect || new Function();
    this.onviewchange = b.onviewchange;
    this.autoFill = b.autoFill;
    if ("undefined" == typeof this.autoFill || this.autoFill === null) {
        this.autoFill = true;
    }
    this.dateStyle = b.dateStyle || "";
    this.weekStyle = b.weekStyle || "";
    this.sideButtonStyle = b.sideButtonStyle;
    this.sideButtonClass = b.sideButtonClass;
    this.calendarCount = b.calendarCount || 2;
    this.selected = b.selected || null;
    var a = this.selected ? this.selected.to : (new Date());
    this.viewDate = new Date(a.getFullYear(), a.getMonth(), 1);
    this.clickState = "from";
    this.calWrapIdPrefix = this.uniqueId + "DSCalendarWrap";
    this.wrapClass = "ds-wrap";
    this.prevClass = "ds-prev";
    this.prevYearClass = "ds-prevyear";
    this.prevMonthClass = "ds-prevmonth";
    this.nextYearClass = "ds-nextyear";
    this.nextMonthClass = "ds-nextmonth";
    this.calWrapClass = "ds-cal-wrap";
    this.calBlankClass = "ds-cal-blank";
    this.calHeadClass = "ds-cal-head";
    this.calHeadSelClass = "ds-cal-head-selected";
    this.dateClass = "ds-date-thismonth";
    this.dateSelectedClass = "ds-date-selected";
    this.dateOtherClass = "ds-date-othermonth";
    this.weekClass = "ds-week";
    this.maxDateRangeDays = b.maxDateRangeDays || 365;
}
DateSelector.getDateCountByMonth = function (a) {
    var b = new Date(a.getFullYear(), a.getMonth() + 1, 0);
    return b.getDate();
};
DateSelector.prototype = {
    setSelected: function (a) {
        this.selected = (a && a.from && a.to) ? a : {};
        this.renderCalendars();
        this.clickState = "from";
        return this;
    },
    getSelected: function () {
        return this.selected;
    },
    appendTo: function (c) {
        var d = document.createElement("table");
        var a = document.createElement("tbody");
        var e = document.createElement("tr");
        d.className = this.wrapClass;
        a.appendChild(e);
        d.appendChild(a);
        c.appendChild(d);
        d.onclick = this.getClickHandler();
        this.renderSide("prev", e);
        for (var b = 0; b < this.calendarCount; b++) {
            var f = document.createElement("td");
            f.vAlign = "top";
            f.id = this.calWrapIdPrefix + b;
            f.className = this.calWrapClass;
            e.appendChild(f);
            if (b < this.calendarCount - 1) {
                f = document.createElement("td");
                f.className = this.calBlankClass;
                e.appendChild(f);
            }
        }
        this.renderSide("next", e);
        this.renderCalendars();
        this.appendTo = new Function();
    },
    renderSide: function (l, k) {
        var a = '<div class="#{yearclass}" sign="#{type}year" style="#{yearstyle}"></div><div class="#{monthclass}" sign="#{type}month" style="#{monthstyle}"></div>';
        var p = ["year", "month"],
            b = ["class", "style"];
        var q = {
            styleFn: this.sideButtonStyle || new Function(),
            classFn: this.sideButtonClass || new Function()
        };
        var h = {
            from: new Date(this.viewDate.getFullYear(), this.viewDate.getMonth() - this.calendarCount + 1, 1),
            to: this.viewDate
        };
        var f = {
            monthstyle: this[l + "MonthStyle"],
            yearstyle: this[l + "YearStyle"],
            monthclass: this[l + "MonthClass"],
            yearclass: this[l + "YearClass"]
        };
        var o, d, n;
        for (var g = 0; g < p.length; g++) {
            o = p[g];
            for (var e = 0; e < b.length; e++) {
                n = q[b[e] + "Fn"];
                switch (typeof n) {
                case "function":
                    h.type = l + o;
                    d = n(h);
                    if (d) {
                        f[o + b[e]] = d;
                    }
                    break;
                case "string":
                    f[o + b[e]] = n;
                    break;
                }
            }
        }
        var m = this.uniqueId + l + "Side";
        var c = adwiser.$(m);
        if (!c) {
            if (!k) {
                return
            }
            c = document.createElement("td");
            c.id = m;
            l = l.toLowerCase();
            c.vAlign = "top";
            c.className = this[l + "Class"];
            k.appendChild(c);
        }
        c.innerHTML = adwiser.format(a, {
            type: l,
            yearclass: f.yearclass,
            monthclass: f.monthclass,
            yearstyle: f.yearstyle,
            monthstyle: f.monthstyle
        });
    },
    renderCalendars: function () {
        for (var b = 0; b < this.calendarCount; b++) {
            var a = new Date(this.viewDate.getFullYear(), this.viewDate.getMonth() - this.calendarCount + 1 + b, 1);
            adwiser.$(this.calWrapIdPrefix + b).innerHTML = this.getCalendarHtml(a.getFullYear(), a.getMonth());
        }
    },
    getCalendarHtml: function (h, g) {
        var e = '<div class="#{clazz}" sign="month" y="#{year}" m="#{month}">#{year}年#{nextmonth}月</div>';
        var c = '<td sign="date" style="#{4}" class="#{3}" y="#{2}" m="#{1}" d="#{0}">#{0}</td>';
        var i = '<table cellpadding="0" cellspacing="0" border="0"><thead>#{thead}</thead><tbody>#{tbody}</tbody></table>';
        var d = [];
        var f = this.calHeadClass;
        var b = new Date(h, g, 1);
        var a = new Date(b);
        a.setMonth(a.getMonth() + 1);
        a.setDate(0);
        if (this.selected && b - this.selected.from >= 0 && a - this.selected.to <= 0) {
            f = this.calHeadSelClass;
        }
        d.push(adwiser.format(e, {
            clazz: f,
            year: h,
            month: g,
            nextmonth: (g + 1)
        }), adwiser.format(i, {
            thead: this.getCalHeadHtml(),
            tbody: this.getCalBodyHtml(h, g)
        }));
        return d.join("");
    },
    getCalHeadHtml: function () {
        var d = ["日", "一", "二", "三", "四", "五", "六"];
        var c = [adwiser.format('<tr><td style="background:#fff;" class="#{0}"></td>', this.weekClass)];
        var a = "<td>#{0}</td>";
        for (var b = 0; b < 7; b++) {
            c.push(adwiser.format(a, d[b]));
        }
        c.push("</tr>");
        return c.join("");
    },
    getCalBodyHtml: function (h, t) {
        var r = '<td sign="#{3}" style="#{5}" class="#{4}" y="#{2}" m="#{1}" d="#{0}">#{6}</td>';
        var n = '<td class="#{0}" sign="week" style="#{1}"></td>';
        var g = [];

        function j() {
            var i = Array.apply(i, arguments);
            i.unshift(r);
            g.push(adwiser.format.apply(null, i));
        }
        var l = new Date(h, t, 1);
        var w = new Date(h, (t - 1), 1);
        var k = DateSelector.getDateCountByMonth(w);
        var c = DateSelector.getDateCountByMonth(l);
        var d = 0 - new Date(h, t, 1).getDay();
        var e, f, u, v, a, b;
        if (this.dateStyle.constructor == String) {
            a = this.dateStyle;
        }
        if ("function" == typeof this.weekStyle) {
            b = this.weekStyle(new Date(w.getFullYear(), w.getMonth(), k + d + 1)) || "";
        }
        g.push("<tr>", adwiser.format(n, this.weekClass, b));
        for (var s = 0; d < c; d++, s++) {
            var q = "date";
            if (s > 0 && s % 7 === 0) {
                if ("function" == typeof this.weekStyle) {
                    b = this.weekStyle(new Date(h, t, d + 1)) || "";
                }
                g.push("</tr><tr>", adwiser.format(n, this.weekClass, b));
            }
            if (d < 0) {
                e = k + d + 1;
                f = w.getMonth();
                u = w.getFullYear();
                v = this.dateOtherClass;
                if (!this.autoFill) {
                    q = "";
                }
            } else {
                e = d + 1;
                f = t;
                u = h;
                v = this.dateClass;
                if (this.selected) {
                    var m = new Date(u, f, e);
                    if (m - this.selected.from >= 0 && m - this.selected.to <= 0) {
                        v = this.dateSelectedClass;
                    }
                }
            }
            if ("function" == typeof this.dateStyle && q) {
                a = this.dateStyle(new Date(u, f, e)) || "";
            }
            j(e, f, u, q, v, (v == this.dateSelectedClass ? "" : a), (q ? e : ""));
        }
        f = t + 1;
        u = h;
        if (f > 11) {
            f = 0;
            u++;
        }
        v = this.dateOtherClass;
        a = "";
        for (var o = s % 7, p = o; o > 0 && o < 7; o++) {
            if (this.autoFill) {
                q = "date";
                e = o - p + 1;
                if ("function" == typeof this.dateStyle) {
                    a = this.dateStyle(new Date(u, f, e)) || "";
                }
            } else {
                e = "&nbsp;";
                q = "";
            }
            j(e, f, u, q, v, a, e);
        }
        g.push("</tr>");
        return g.join("");
    },
    getClickHandler: function () {
        var a = this;
        return function (k) {
            k = k || window.event;
            var j = k.srcElement || k.target;
            var c = j.getAttribute("sign");
            var m = new Date(a.viewDate);
            var f, n, l, i;
            switch (c) {
            case "prevmonth":
                m.setMonth(a.viewDate.getMonth() - 1);
                break;
            case "prevyear":
                m.setFullYear(a.viewDate.getFullYear() - 1);
                break;
            case "nextmonth":
                m.setMonth(a.viewDate.getMonth() + 1);
                break;
            case "nextyear":
                m.setFullYear(a.viewDate.getFullYear() + 1);
                break;
            case "date":
                var b = new Date(j.getAttribute("y"), j.getAttribute("m"), j.getAttribute("d"));
                if (a.clickState == "from") {
                    f = {
                        from: b,
                        to: b
                    };
                } else {
                    f = {
                        from: a.selected.from,
                        to: b
                    };
                    if (f.to - f.from < 0) {
                        var g = f.to;
                        f.to = f.from;
                        f.from = g;
                    }
                }
                break;
            case "week":
                var d = j.nextSibling;
                l = new Date(d.getAttribute("y"), d.getAttribute("m"), d.getAttribute("d"));
                i = new Date(l);
                i.setDate(i.getDate() + 6);
                f = {
                    from: l,
                    to: i
                };
                break;
            case "month":
                l = new Date(j.getAttribute("y"), j.getAttribute("m"), 1);
                i = new Date(l);
                i.setMonth(i.getMonth() + 1);
                i.setDate(0);
                f = {
                    from: l,
                    to: i
                };
                break;
            default:
                return
            }
            switch (c) {
            case "prevmonth":
            case "prevyear":
            case "nextmonth":
            case "nextyear":
                if (a.onviewchange) {
                    var h = new Date(m);
                    h.setMonth(h.getMonth() - a.calendarCount + 1);
                    n = a.onviewchange.call(a, {
                        from: h,
                        to: m
                    });
                    if (n === false) {
                        return
                    }
                }
                a.viewDate = m;
                a.renderSide("prev");
                a.renderSide("next");
                break;
            case "date":
            case "week":
            case "month":
                n = a.onselect.call(a, f);
                if (n === false) {
                    return
                }
                a.selected = f;
                a.clickState = (c == "date" && a.clickState == "from") ? "to" : "from";
                break;
            }
            a.renderCalendars();
        };
    }
};


function DateController(a) {
    this.dateSelectBar = a.container || "DateSelectBar";
    var b = adwiser.random();
    this.calendarContainerId = "calendarContainer" + b;
    this.maskIframeId = "Iframe" + b;
    this.calendarContainerClass = a.calendarContainerClass || "calendar-box";
    this.toolTipClass = a.toolTipClass || "date-select-tip";
    this.btnBarClass = a.toolTipClass || "btn-bar";
    this.onchange = a.onchange || {};
    this.compareChange = a.compareChange || {};
    this.startDate = a.startDate || new Date(2011, 0, 1);
    this.endDate = a.endDate || new Date();
    this.otherStartDate = a.otherStartDate || null;
    this.otherEndDate = a.otherEndDate || null;
    this.todayDate = a.todayDate || new Date();
    this.firstDate = a.firstDate || new Date(2011, 0, 1);
    this.dateCompare = false;
    this.msg = "";
    this.dateSelector = null;
    this.diff;
    this.selected = {};
    this.maxDateRangeDays = a.maxDateRangeDays || 365;
    
    //所有属性定义放在此方法调用之前
    this.init();
    
}
DateController.prototype = {
	// ie7 浏览器下 两个 div 之间不能有空格，否则会换行，不会在一行之间显示
    tpl: '<iframe src="about:blank" frameborder="0" scrolling="no" id="#{maskIframeId}"></iframe><div></div><div class="#{toolTipClass}"></div><div class="#{btnBarClass}"><div id="__date_btn_ok" class="date_sel_ok" title="确定"></div><div id="__date_btn_cancel" class="date_sel_cancel" title="取消"></div></div>',
    init: function () {
        var b = adwiser.$(this.dateSelectBar).getElementsByTagName("A");
        for (var a = 0, c = b.length; a < c; a++) {
            b[a].onclick = this.selectHandle();
            //////////////////
        }
        
        //alert('init: ' +  this.maxDateRangeDays);
        
        this.createCalendarContainer();
        this.createCalendar();
        this.initInputDate();
        this.initShortcutDate();
        this.bindHandle();
    },
    bindHandle: function () {
        var e = adwiser.$;
        var a = e(this.dateSelectBar);
        var f = e(this.calendarContainerId);
        var c = a.getElementsByTagName("INPUT");
        var b = a.getElementsByTagName("IMG");
        c[0].onclick = this.showDateBox(0);
        c[2].onclick = this.showDateBox(2);
        b[0].onclick = this.showDateBox(0);
        b[1].onclick = this.showDateBox(2);
        c[1].onclick = this.showDustDateInput();
        adwiser.on(f, "click", stopBubble);
        //var d = f.getElementsByTagName("INPUT");
        
        var btn_ok = document.getElementById('__date_btn_ok');
        var btn_cancel = document.getElementById('__date_btn_cancel');
        
        
        btn_ok.onclick = this.submitDateSelect();
        btn_cancel.onclick = this.hideDateBoxHandle();
    },
    showDustDateInput: function () {
        var a = this;
        return function (b) {
            stopBubble(b);
            if (this.checked) {
                a.showInput();
            } else {
            	
            	//alert('showDustDateInput before hideInput');
                a.hideInput();
                a.otherStartDate = null;
                a.otherEndDate = null;
                a.msg = "";
                a.initInputDate();
                a.onchange(this.id);
            }
        };
    },
    submitDateSelect: function () {
        var a = this;
        return function () {
            var c = a.selected.from;
            var b = a.selected.to;
            if (a.dateCompare) {
                a.otherStartDate = c;
                a.otherEndDate = b;
            } else {
                a.startDate = c;
                a.endDate = b;
            }
            if (typeof(TIP) != "undefined") {
                TIP.clearMsg = false;
            }
            a.initInputDate();
            a.onchange("submitDateSelect");
            a.initShortcutDate();
            adwiser.hide(a.calendarContainerId);
        };
    },
    setDate: function (a, b) {
        this.startDate = a;
        this.endDate = b;
        if (typeof(TIP) != "undefined") {
            TIP.clearMsg = false;
        }
        this.initInputDate();
        this.onchange("submitDateSelect");
        this.initShortcutDate();
    },
    hideDateBoxHandle: function () {
        var a = this;
        return function () {
            adwiser.un(document, "click", a.hideDateBoxHandle());
            adwiser.hide(a.calendarContainerId);
            //不再初始化 by zhaidw
            //a.initInputDate();
            
            a.initInputDate2();
            
            
            
            
            
            
            
            
            
            
        };
    },
    showDateBox: function (a) {
        var b = this;
        return function (j) {
            stopBubble(j);
            if (typeof(FlashCheckController) != "undefined" && FlashCheckController.checkBox.disableSelect) {
                return
            }
            stopDefault(j);
            b.msg = "";
            var r = adwiser.$;
            var q = adwiser.date.format;
            var f = adwiser.date.parse;
            var c = r(b.calendarContainerId);
            var s = adwiser.dom.getPosition;
            b.dateCompare = (a == 2) ? true : false;
            b.diff = (a == 2) ? b.dateDiff(b.startDate, b.endDate) : 0;
            c.style.display = "block";
            b.dateSelector.clickState = "from";
            var o = (a == 2) ? b.otherStartDate : b.startDate;
            var p = (a == 2) ? b.otherEndDate : b.endDate;
            b.selected.from = o;
            b.selected.to = p;
            if (o) {
                o = b.parseDate(formatDate("YYYY-MM-DD", o));
                p = b.parseDate(formatDate("YYYY-MM-DD", p));
                b.dateSelector.setSelected({
                    from: o,
                    to: p
                });
            } else {
                b.dateSelector.setSelected({});
            }
            var k = (this.tagName == "IMG") ? adwiser.dom.prev(this) : this;
            var g = (a == 2) ? -300 : 0;
            var m = s(k).left + g;
            var l = s(k).top + 27; // input bottom
            c.style.top = l + "px";
            c.style.left = m + "px";
            adwiser.on(document, "click", b.hideDateBoxHandle());
            if (adwiser.ie == 6) {
                var d = r(b.maskIframeId);
                d.style.display = "block";
                var i = c.offsetHeight;
                var n = c.offsetWidth;
                d.style.height = (i - 5) + "px";
                d.style.width = n + "px";
            }
        };
    },
    initInputDate: function () {
        var b = adwiser.$(this.dateSelectBar);
        var g = b.getElementsByTagName("INPUT");
        var i = adwiser.date.format;
        var j = i(this.startDate, "yyyy-MM-dd");
        var h = i(this.endDate, "yyyy-MM-dd");
        var c = (j == h) ? h : j + " 至 " + h;
        g[0].value = c;
        if (this.otherStartDate && this.otherStartDate.constructor == Date) {
            g[1].checked = true;
            
            //alert(' initInputDate showInput');
            	
            this.showInput();
            var d = i(this.otherStartDate, "yyyy-MM-dd");
            var a = i(this.otherEndDate, "yyyy-MM-dd");
            g[2].value = (d == a) ? a : d + " 至 " + a;
        } else {
            g[1].checked = false;
            g[2].value = "";
            //alert('initInputDate before hideInput');
            this.hideInput();
        }
    },
    
    //鼠标离开日期值选择层是的监听事件
    initInputDate2: function () {
    	
    	//alert('initInputDate2');
    	
        var b = adwiser.$(this.dateSelectBar);
        var g = b.getElementsByTagName("INPUT");
        var i = adwiser.date.format;
        var j = i(this.startDate, "yyyy-MM-dd");
        var h = i(this.endDate, "yyyy-MM-dd");
        var c = (j == h) ? h : j + " 至 " + h;
        g[0].value = c;
        if (this.otherStartDate && this.otherStartDate.constructor == Date && g[1].checked == true) {
            //g[1].checked = true;
            
            //alert(' initInputDate showInput');
            	
            this.showInput();
            var d = i(this.otherStartDate, "yyyy-MM-dd");
            var a = i(this.otherEndDate, "yyyy-MM-dd");
            g[2].value = (d == a) ? a : d + " 至 " + a;
            
        } else {
            //g[1].checked = false;
            g[2].value = "";
            //alert('initInputDate before hideInput');
            //this.hideInput();
        }
    },
    
    initShortcutDate: function () {
        var a = adwiser.$(this.dateSelectBar);
        var m = a.getElementsByTagName("A");
        var c = m.length;
        var f = this.endDate;
        var b = this.startDate;
        var o = this.todayDate;
        this.removeClass();
        var l = this.dateDiff;
        var j = l(f, o);
        var d = l(b, f);
        var e = Number.MAX_VALUE;
        if (j == -1 && d == 0) {
            e = -1;
        }
        if (j == 0) {
            e = d;
        }
        for (var g = 0, c = m.length; g < c; g++) {
        	var k = m[g].href.indexOf("#");

            
            //alert(g + ' : ' + k + ' customDate: ' + customDate);
            if (k != -1) {
                var h = parseInt(m[g].href.substr(k + 1));
                if (h == e ) {
                    m[g].className = "cur";
                    break;
                }else{
                	
                	/////////////////// 高亮当前 快捷日期选择 /////////////////////
                    var i = m[g].href.lastIndexOf("#") + 1;
                    var customDate = m[g].href.substring(i);
                    var Nowdate = this.todayDate;
                    var aDay = 86400000;
                    if(customDate == 'thisweek'){
                    	var WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*aDay);
                    	var WeekLastDay=new Date((WeekFirstDay/1000+6*86400)*1000);
                    	if(this.startDate.getTime() == WeekFirstDay.getTime() && this.endDate.getTime() == WeekLastDay.getTime()){
                        	m[g].className = "cur";
                            break;
                    	}
                    }
                    
                    if(customDate == 'lastweek'){
                    	var WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()+6)*aDay);
                    	var WeekLastDay=new Date((WeekFirstDay/1000+6*86400)*1000);  

                    	if(this.startDate.getTime() == WeekFirstDay.getTime() && this.endDate.getTime() == WeekLastDay.getTime()){
                        	m[g].className = "cur";
                        	
                        	
                            break;
                    	}
                    	
                    }
                    if(customDate == 'thismonth'){
                    	var MonthFirstDay=new Date(Nowdate.getFullYear(),Nowdate.getMonth(),1);   

                    	if(this.startDate.getTime() == MonthFirstDay.getTime() && this.endDate.getTime() == Nowdate.getTime()){
                        	m[g].className = "cur";
                            break;
                    	}
                    	
                    }
                    
                    if(customDate == 'lastmonth'){
                    	var MonthFirstDay=new Date(Nowdate.getFullYear(),Nowdate.getMonth()-1,1);
                    	var MonthNextFirstDay=new Date(Nowdate.getFullYear(),Nowdate.getMonth(),1);
                    	var MonthLastDay=new Date(MonthNextFirstDay-aDay);
                    	   
                    	if(this.startDate.getTime() == MonthFirstDay.getTime() && this.endDate.getTime() == MonthLastDay.getTime()){
                    		
                        	m[g].className = "cur";
                        	
                           break;
                    	}
                    }
                    
                    /////////////////// 高亮当前 快捷日期选择 /////////////////////
                	
                	 
                }
            }
            
            
        }
    },
    showInput: function () {
        var a = adwiser.$(this.dateSelectBar);
        var c = a.getElementsByTagName("INPUT");
        var b = a.getElementsByTagName("IMG")[1];
        c[1].checked = true;
        c[2].style.display = "";
        c[2].value = "";
        b.style.display = "";
        
        //alert('showInput ');
    },
    hideInput: function () {
        var a = adwiser.$(this.dateSelectBar);
        var c = a.getElementsByTagName("INPUT");
        var b = a.getElementsByTagName("IMG")[1];
        c[1].checked = false;
        c[2].style.display = "none";
        b.style.display = "none";
        
        //alert('hideInput ');
    },
    createCalendar: function () {
        var c = adwiser.$(this.calendarContainerId);
        var b = c.getElementsByTagName("DIV");
        var e = b[1];
        var h = this.todayDate;
        var d = this.firstDate;
        var f = this.todayDate.getTime();
        var maxdays = this.maxDateRangeDays;
        
        //alert('maxdays ' + maxdays);
        //一天的毫秒数
        var a = 3600 * 24 * 1000;
        var i = adwiser.date.format;
        var g = this;
        this.dateSelector = new DateSelector({
        	maxDateRangeDays : maxdays,
        	//显示日历的个数
            calendarCount: 3,
            autoFill: 0,
            onviewchange: function (k) {
                var j = new Date(f + a * 31);
                j.setDate(1);
                if (k.to - j > 0) {
                    return false;
                }
            },
            dateStyle: function (j) {
                if (g.dateCompare) {
                    var k = g.diff ? Math.abs(Math.floor(g.diff)) + 1 : 1;
                    if (this.clickState == "to" && Math.abs(this.selected.from - j) < a * k) {
                        return "background:#fbeaa7";
                    }
                }
                if (j - h > 0 || j - d < 0) {
                    return "color:#999;cursor:default;background:#fff";
                }
                if (i(j, "yyyy-MM-dd") == i(h, "yyyy-MM-dd")) {
                    return "font-weight:bold; border:1px solid #a7aebc";
                }
            },
            sideButtonClass: function (j) {
                var k = h;
                if (j.type == "nextmonth" && j.to >= new Date(k.getFullYear(), k.getMonth(), 1)) {
                    return "ds-nextmonth-ed";
                }
            }
        });
        this.dateSelector.viewDate = new Date(f - a * 1);
        this.dateSelector.onselect = function (j) {
            var m = j.from;
            var l = j.to;
            if (h - l < 0) {
                e.style.display = "block";
                e.innerHTML = "您所选日期内暂无数据，请重新选择。";
                return false;
            }
            if ((l - d) < 0 || (m - d) < 0) {
                e.style.display = "block";
				e.innerHTML = "您所选日期内暂无数据，请重新选择。";
                return false;
            }
            var k = (l - m) / a;
            
            //alert('l: ' + l + 'm: ' + m  + ', k: ' + k );
            
            if ( (k+1) > maxdays ) {
                e.style.display = "block";
                e.innerHTML = "时间范围查询不能超出 " + maxdays + " 天。";
                myDateSelector.clickState = "from";
                return false;
            }
            e.innerHTML = "";
            g.selected = j;
            g.onSelect();
        };
        this.dateSelector.appendTo(b[0]);
    },
    onSelect: function () {
        var a = adwiser.$(this.dateSelectBar);
        var c = a.getElementsByTagName("INPUT");
        var f = adwiser.date.format;
        var d = this.selected;
        var g = this.dateCompare ? c[2] : c[0];
        var e = f(d.from, "yyyy-MM-dd");
        var b = f(d.to, "yyyy-MM-dd");
        g.value = (e == b) ? b : e + " 至 " + b;
    },
    createCalendarContainer: function () {
        var c = document.createElement("DIV");
        c.id = this.calendarContainerId;
        c.className = this.calendarContainerClass;
        c.innerHTML = adwiser.format(this.tpl, {
            maskIframeId: this.maskIframeId,
            toolTipClass: this.toolTipClass,
            btnBarClass: this.btnBarClass
        });
        document.body.appendChild(c);
    },
    selectHandle: function (a) {
        var b = this;
        return function (h) {
            if (typeof(FlashCheckController) != "undefined" && FlashCheckController.checkBox.disableSelect) {
                return
            }
            stopDefault(h);
            b.msg = "";
            //////////////自定义日期段
            var i = this.href.lastIndexOf("#") + 1;
            var f = parseInt(this.href.substring(i), 10);
            var customDate = this.href.substring(i);
            var Nowdate = b.todayDate; //new Date();
            var aDay = 86400000;
            if(customDate == 'thisweek'){
            	var WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*aDay);
            	var WeekLastDay=new Date((WeekFirstDay/1000+6*86400)*1000);
            	b.startDate = WeekFirstDay;
            	b.endDate = WeekLastDay;
            }else if(customDate == 'lastweek'){
            	var WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()+6)*aDay);
            	var WeekLastDay=new Date((WeekFirstDay/1000+6*86400)*1000);  
            	b.startDate = WeekFirstDay;
            	b.endDate = WeekLastDay;
            	
            }else if(customDate == 'thismonth'){
            	var MonthFirstDay=new Date(Nowdate.getFullYear(),Nowdate.getMonth(),1);   
            	b.startDate = MonthFirstDay;
            	b.endDate = Nowdate;
            	
            }else if(customDate == 'lastmonth'){
            	var MonthFirstDay=new Date(Nowdate.getFullYear(),Nowdate.getMonth()-1,1);
            	var MonthNextFirstDay=new Date(Nowdate.getFullYear(),Nowdate.getMonth(),1);
            	var MonthLastDay=new Date(MonthNextFirstDay-aDay);
            	   
            	b.startDate = MonthFirstDay;
            	b.endDate = MonthLastDay;
            }
            
            else if (f != 0) {
            	
            	//修改时间 昨天 最近7天等
            	
                var c = (c == -1) ? -1 : f;
                b.startDate = new Date(b.recentDate(c));
                var g = (f == -1) ? f : 0;
                b.endDate = new Date(b.recentDate(g));
                
                
                
            } else {
                var d = b.todayDate;
                b.startDate = d;
                b.endDate = d;
            }
            
            
            b.otherStartDate = "";
            b.otherEndDate = "";
            b.initInputDate();
            b.removeClass();
            this.className = "cur";
            b.onchange();
        };
    },
    removeClass: function () {
        var b = adwiser.$(this.dateSelectBar).getElementsByTagName("A");
        for (var a = 0, c = b.length; a < c; a++) {
            b[a].className = "";
        }
    },
    parseDate: function (a) {
        var b = a.indexOf("-");
        if (b > -1) {
            var c = a.split("-");
            return new Date(c[0], c[1] - 1, c[2]).getTime();
        } else {
            return {};
        }
    },
    dateDiff: function (b, a) {
        b.setHours(0);
        b.setMinutes(0);
        b.setSeconds(0);
        b.setMilliseconds(0);
        a.setHours(0);
        a.setMinutes(0);
        a.setSeconds(0);
        a.setMilliseconds(0);
        return parseInt((b - a) / 1000 / 60 / 60 / 24, 10);
    },
    recentDate: function (a) {
        return this.todayDate.getTime() + a * 3600 * 24 * 1000;
    }
};

//var DC = new DateController({
//    todayDate: new Date(+VAR.TODAY_TIME),
//    firstDate: new Date(+VAR.START_TIME),
//    startDate: new Date(+PARAMS.s_date),
//    endDate: new Date(+PARAMS.e_date),
//    otherStartDate: PARAMS.s2_date != "" ? new Date(+PARAMS.s2_date) : "",
//    otherEndDate: PARAMS.s2_date != "" ? new Date(+PARAMS.e2_date) : "",
//    onchange: dateOnchange
//});

//function dateOnchange(d) {
//
//	alert('日期被修改了, d = ' + d);
//
//    PARAMS.s_date = this.startDate.getTime();
//    PARAMS.e_date = this.endDate.getTime();
//    var c = this.otherStartDate && this.otherStartDate.constructor == Date ? true : false;
//
//    PARAMS.s2_date = c ? this.otherStartDate.getTime() : "";
//    PARAMS.e2_date = c ? this.otherEndDate.getTime() : "";
//
//	if (d == "RadioDateBtn") {
//		var b = adwiser.$("RadioDateBtn");
//		if (b.checked) {
//			alert('RadioDateBtn checked');
//		} else {
//			alert('RadioDateBtn unchecked');
//		}
//	}else if(d == "submitDateSelect"){
//		alert('submitDateSelect');
//	
//	}else{
//		alert('使用前面的连接选择日期');
//	}
//}


Date.prototype.format = function(format)
{
    var e = ["星期天","星期一","星期二","星期三","星期四","星期五","星期六"];
    var o =
    {
        "M+" : this.getMonth()+1,                  //month
        "d+" : this.getDate(),                     //day
        "h+" : this.getHours(),                    //hour
        "m+" : this.getMinutes(),                  //minute
        "s+" : this.getSeconds(),                  //second
        "q+" : Math.floor((this.getMonth()+3)/3), //quarter
        "S" : this.getMilliseconds()               //millisecond
    };
    
    if(/(y+)/.test(format))
    format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length));
    if(/(e)/.test(format)) format=format.replace(RegExp.$1,e[this.getDay()]);
    for(var k in o)
    if(new RegExp("("+ k +")").test(format))
    format = format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
    return format;
};
